﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Dack_Web2
{
    public class User
    {
        string _tenDN, _Password, _MaUser;

        public int MaUser
        {
            get {
                _MaUser = User.FromId(_tenDN)._MaUser;
                return int.Parse(_MaUser); 
            }
        }
        private GioHang _cart;

        public GioHang Cart
        {
            get { return _cart; }
            set { _cart = value; }
        }
        //...
        /// <summary>
        /// Khởi tạo giỏ hàng rỗng cho user
        /// </summary>
        public void InitCart()
        {
            _cart = new GioHang();
        }

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public string TenDN
        {
            get { return _tenDN; }
            set { _tenDN = value; }
        }

        public User(string Ten, string Pass)
        {
            _tenDN = Ten;
            _Password = Pass;
        }

        
        public bool IsValidLogin()
        {
            string sql = "select * from [User] where (TenDangNhap='" + _tenDN.Replace("'", "''") +
                "') And (MatKhau ='" + _Password.Replace("'", "''") + "')";
            using (DataTable dt = Sql_DataProvider.SQL_ExecuteTable(sql))
            {
                bool ret = dt.Rows.Count > 0;
                return ret;
            }
        }

        public static User FromId(string tenDN)
        {
            string sql = "select * from [User] where TenDangNhap='" + tenDN.Replace("'","''") + "'";
            using (DataTable dt = Sql_DataProvider.SQL_ExecuteTable(sql))
            {
                DataRow row = dt.Rows[0];
                string matKhau = row["MatKhau"].ToString();
                User ret = new User(tenDN, matKhau);
                ret._MaUser = row["MaUser"].ToString();
                return ret;
            }
        }
                
    }

}